package com.xiaodimall.backsys.controller;
import com.xiaodimall.backsys.common.CascadeSelectionResult;
import com.xiaodimall.backsys.common.JsonResult;
import com.xiaodimall.backsys.common.PageResult;
import com.xiaodimall.backsys.pojo.GoodsInfo;
import com.xiaodimall.backsys.service.GoodsCategoryService;
import com.xiaodimall.backsys.service.GoodsInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.security.auth.login.CredentialNotFoundException;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Author: CJJ
 * @Date: Created in 2022/11/25 16:53
 * @Description: 商品操作
 */
@Controller
@RequestMapping("/admin/")
public class AdminGoodsController {

    /**
     * @Author: CJJ
     * @Description: 商品信息业务层对象
     */
    @Resource
    private GoodsInfoService goodsInfoService;

    /**
     * @Author: CJJ
     * @Description: 商品分类业务层对象
     */
    @Resource
    private GoodsCategoryService goodsCategoryService;


    /**
     * @Author: CJJ
     * @param currentPage 当前页
     * @param pageSize    查询记录数
     * @return 自定义类型: 包含记录集合, 总记录数
     * @Description: 分页查询
     */
    @GetMapping("/goods/{currentPage}/{pageSize}")
    @ResponseBody
    public PageResult<GoodsInfo> getAllGoodsInfoByPage(@PathVariable("currentPage") String currentPage, @PathVariable("pageSize") String pageSize) {
        return goodsInfoService.getAllGoodsInfoByPage(currentPage, pageSize);
    }

    /**
     * @Author: CJJ
     * @param goodsInfo 商品实体类
     * @return JR
     * @Description: 修改商品信息
     */
    @PutMapping("/goods")
    @ResponseBody
    public JsonResult updateGoodsInfo(GoodsInfo goodsInfo) {
        return goodsInfoService.updateGoodsInfo(goodsInfo);
    }

    /**
     * @Author: CJJ
     * @return 自定义对象: 包含商品类型 id, 名称, 以及该类型子项(如果有)
     * @Description: 获取商品类型, 封装到级联选择器的数据中
     */
    @GetMapping("/goods/type")
    @ResponseBody
    public List<CascadeSelectionResult> getSelectionData() {
        return goodsCategoryService.getSelectionData();
    }

    /**
     * @Author: CJJ
     * @param multipartFile #
     * @return #
     * @Description: 商品图片上传
     */
    @PostMapping("/goods/upload/img")
    @ResponseBody
    public JsonResult uploadGoodsImg(@RequestParam("goodsCoverImg") MultipartFile multipartFile) {
        JsonResult jsonResult = null;
        String uploadAbsolutePath = "F:\\springbootproject\\download\\XiaoDiMall\\src\\main\\resources\\static\\admin\\goods-img";
        if(!multipartFile.isEmpty()){
            try{
                multipartFile.transferTo(new File(uploadAbsolutePath +"\\"+ multipartFile.getOriginalFilename()));
                jsonResult = new JsonResult(200,"上传成功",multipartFile.getOriginalFilename());
            }catch(Exception e){
                e.printStackTrace();
                jsonResult = new JsonResult(500,"网络异常","");
            }
        }else{
            jsonResult = new JsonResult(404,"上传失败","");
        }
        return jsonResult;
    }


    /**
     * @Author: CJj
     * @param goodsInfo 商品信息实体
     * @return JR
     * @Description: 添加商品
     */
    @PostMapping("/goods")
    @ResponseBody
    public JsonResult addGoods(GoodsInfo goodsInfo){
        return goodsInfoService.addGoodsService(goodsInfo);
    }

    /**
     * @Author: CJJ
     * @param originalPrice 商品标价
     * @return 商城售价标记点
     * @Description: 根据传入的商品标价实时计算各个折扣标记点数值
     */
    @GetMapping("/goods/marks/{originalPrice}")
    @ResponseBody
    public Map<Integer,String> getMarks(@PathVariable("originalPrice") Integer originalPrice){
        return goodsInfoService.getMarks(originalPrice);
    }

    /**
     * @Author: CJJ
     * @param goodsId 商品ID
     * @return JR
     * @Description: 删除单个商品
     */
    @DeleteMapping("/goods")
    @ResponseBody
    public JsonResult deleteGoodsById(Integer goodsId){
        return goodsInfoService.deleteGoodsById(goodsId);
    }


    /**
     * @Author: CJJ
     * @param goodsIds 商品ID数组
     * @return JR
     * @Description: 批量删除商品
     */
    @DeleteMapping("/goods/list")
    @ResponseBody
    public JsonResult deleteGoodsByIds(@RequestParam List<Integer> goodsIds){
        return goodsInfoService.deleteGoodsByIds(goodsIds);
    }

    /**
     * 商品搜索
     * @param column 要搜索的表字段
     * @param value 搜索的值
     * @return JR
     */
    @GetMapping("/goods/search/{column}/{value}")
    @ResponseBody
    public JsonResult selectByCondition(@PathVariable("column") String column,@PathVariable("value") String value){
        return goodsInfoService.selectGoodsBuCondition(column,value);
    }

    @PutMapping ("/goods/status/start")
    @ResponseBody
    public JsonResult startSellSelectGoods(@RequestParam List<Integer> goodsIds){
        return goodsInfoService.startSellSelectGoods(goodsIds);
    }

    @PutMapping("/goods/status/stop")
    @ResponseBody
    public JsonResult stopSellSelectGoods(@RequestParam List<Integer> goodsIds){
        return goodsInfoService.stopSellSelectGoods(goodsIds);
    }



}
